<?php

namespace app\common\model;

use think\Model;

/**
 * Class trRentalOutlets
 * @property trOutletsCar $carList
 * @property int status
 * @property string name
 * @package app\common\model
 * 租车网点表
 */
class trRentalOutlets extends Model
{
    protected $pk = 'id';
    protected $name = "rental_outlets"; //表名

    protected $type = [
        'create_time'  =>  'timestamp:Y-m-d H:i:s',
        'update_time'  =>  'timestamp:Y-m-d H:i:s',
    ];

    public function carList(){
        return $this->hasMany(trOutletsCar::class, "outlet_id", "id");
    }

    public static function getNumberId(){
        $numberId = trRentalOutlets::where("mall_id", request()->mallData["id"])
            ->where("number_id", "<>", "")
            ->order("id", "desc")
            ->limit(1)
            ->value("number_id");
        if(!$numberId) {
            $numberId = "0000001";
        }else{
            $numberId = $numberId + 1;
            if(strlen($numberId) < 7){
                $numberId = str_pad($numberId, 7, 0, STR_PAD_LEFT);
            }
        }
        return $numberId;
    }

    protected static function init(){
        self::afterDelete(function ($model) {
            trOutletsCar::where("outlet_id", $model->id)->delete();
        });
    }
}